package com.view;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.adpter.JobInfoListAdapter;
import com.android.ocean.util.common.StringUtil;
import com.android.ocean.view.XListView;
import com.android.ocean.view.XListView.IXListViewListener;
import com.http.JobListSearchTask;
import com.matang.R;
import com.modle.JobStruct;
import com.util.AppUtil;

/**
 * 
 * @author zhuwei可下拉加载的内容列表ListView,招聘信息
 * 
 */
public class XJobListView extends BaseFrameLayout implements
		IXListViewListener, JobListSearchTask.CallBack {
	ArrayList<JobStruct> dataList = new ArrayList<JobStruct>();
	int currentPage = 1;
	JobInfoListAdapter xAdapter;
	XListView xListView;
	String catID;// 获取内容的栏目ID
	String searType;// 搜索排序方式
	HashMap<String, Object> conditionMap;

	public XJobListView(Context context) {
		super(context);
		init(context);
	}

	public XJobListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public XJobListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context ctx) {
		this.context = ctx;
		View view = LayoutInflater.from(context).inflate(
				R.layout.l_merge_xlist_conlist, this);
		xListView = (XListView) view.findViewById(R.id.con_listview);
		xListView.setPullLoadEnable(false);
		xListView.setPullRefreshEnable(false);
		xListView.setXListViewListener(this);
		xListView.setRefreshTime(StringUtil.getFreshTime());
		xListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				AppUtil.jobSwitch(context, dataList.get(pos-1));
			}
		});
		loadingView = (View) view.findViewById(R.id.loading_view);

	}

	public void initData(HashMap<String, Object> conditionMap) {
		this.conditionMap = conditionMap;
		flushData();
		loadConList();
	}

	/**
	 * 加载数据
	 */
	private void loadConList() {
		showLoading(loadingView);
		new JobListSearchTask(this).execute(conditionMap);
	}

	/**
	 * 此控件可以复用，当再次initConlistCatID或刷新前时需要先清除数据
	 */
	private void flushData() {
		currentPage = 1;
		xListView.setAdapter(null);
		xListView.setPullLoadEnable(false);
		xListView.setPullRefreshEnable(false);
		xListView.setRefreshTime(StringUtil.getFreshTime());
		
		dataList.clear();
		if(xAdapter!=null){
			xAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onRefresh() {
		flushData();
		loadConList();
	}

	@Override
	public void onLoadMore() {
		currentPage++;
		loadConList();
	}

	private void fillList() {
		if (StringUtil.isListNull(dataList)) {
			AppUtil.tsMsg(context, "无数据...");
		} else {

				xAdapter = new JobInfoListAdapter(dataList, context);
				xListView.setAdapter(xAdapter);

		}

	}

	@Override
	public void procError(String msg) {
		disLoading(loadingView);
		AppUtil.tsMsg(context, msg);

	}

	@Override
	public void procJobList(ArrayList<JobStruct> dataList) {
		disLoading(loadingView);
		this.dataList = dataList;

		fillList();

	}

}
